package com.zcwin.runner.utils;


/**
 * <li>功能描述：缓存管理
 * 
 * @author wsjjiang
 * 
 */
public class CacheManager
{
    private static RedisConfig config;
    
    private static RedisManager rmc;

    private CacheManager()
    {

    }

    private static CacheManager instance = new CacheManager();

    public static CacheManager getInstance()
    {
        if(rmc==null)
        {
            rmc = new RedisManager(config);
            //rmc.clear();
        }
        return instance;
    }

    /**
     * 获取某缓存内容
     * 
     * @param key 缓存主键
     * @return Object 缓存内容
     */
    public Object getFromCache(String key)
    {
        System.out.println("useRedis="+key);
        return rmc.get(key);
    }

    /**
     * 设置某缓存内容
     * 
     * @param key 缓存主键
     * @param value 缓存内容
     */
    public void putInCache(String key, String value)
    {
        if("".equals(key))
        {
            return;
        }
    	System.out.println("Add Redis="+key);
    	//logger.error("Add Redis="+key);
        rmc.add(key, value);
    }
    
    /**
     * 获取某缓存内容
     * 
     * @param key 缓存主键
     * @return Object 缓存内容
     */
    public String getListFromCache(String key)
    {
    	System.out.println("useRedis="+key);
    	return rmc.getList(key);
    }
    
    /**
     * 设置某缓存内容
     * 
     * @param key 缓存主键
     * @param value 缓存内容
     */
    public boolean putInListCache(String key, String value)
    {
    	if("".equals(key))
    	{
    		return true;
    	}
    	System.out.println("Add Redis="+key);
    	//logger.error("Add Redis="+key);
    	return rmc.addList(key, value);
    }
    
    static
    {
        config = new RedisConfig();
        config.setRedisServer(ConfigManager.getInstance().getConfigItem("redis.addr", "127.0.0.1").toString());
        config.setRedisPort(Integer.parseInt(ConfigManager.getInstance().getConfigItem("redis.port", "6379").toString()));
        config.setRedisAuth(ConfigManager.getInstance().getConfigItem("redis.auth", "").toString());
        config.setRedisMax_total(Integer.parseInt(ConfigManager.getInstance().getConfigItem("redis.max_total", "512").toString()));
        config.setRedisMax_idle(Integer.parseInt(ConfigManager.getInstance().getConfigItem("redis.max_idle", "50").toString()));
        config.setRedisMax_wait(Integer.parseInt(ConfigManager.getInstance().getConfigItem("redis.max_wait", "100000").toString()));
        config.setRedisTimeout(Integer.parseInt(ConfigManager.getInstance().getConfigItem("redis.timeout", "100000").toString()));
        config.setRedisTest_on_borrow(Boolean.parseBoolean(ConfigManager.getInstance().getConfigItem("redis.test_on_borrow", "true").toString()));
    }
}
